package com.qintian.rainstorm.test;

import java.util.HashMap;
import java.util.Map;

public class WindGradeUtil {
	
	public static String weatherCondition(String code) {
		final Map<String, String> weathers = new HashMap<>();
		weathers.put("0", "晴");weathers.put("1", "晴");weathers.put("2", "少云");
		weathers.put("3", "多云");weathers.put("4", "阴天");weathers.put("5", "小雨");
		weathers.put("6", "中雨");weathers.put("7", "大雨");weathers.put("8", "暴雨");
		weathers.put("9", "大暴雨");weathers.put("10", "特大暴雨");weathers.put("11", "雷阵雨");
		weathers.put("12", "浮尘");weathers.put("13", "扬沙");weathers.put("14", "小雪");
		weathers.put("15", "小雪");weathers.put("16", "中雪");weathers.put("17", "大雪");
		weathers.put("18", "暴雪"); weathers.put("19", "冻雨"); weathers.put("20", "沙尘暴");
		weathers.put("21", "雷阵雨伴有冰雹"); weathers.put("22", "强沙尘暴"); weathers.put("23", "雾");
		weathers.put("24", "阵雪"); weathers.put("25", "阵雨"); weathers.put("26", "小雨到中雨");
		weathers.put("27", "中雨到大雨"); weathers.put("28", "大雨到暴雨"); weathers.put("29", "暴雨到大暴雨");
		weathers.put("30", "大暴雨到特大暴雨"); weathers.put("31", "小雪到中雪"); weathers.put("32", "中雪到大雪");
		weathers.put("33", "大雪到暴雪"); weathers.put("53", "霾"); weathers.put("-999", "-");
		
		return weathers.get(code);
	}
	
	public static String weatherCode(String condition) {
		final Map<String, String> weathers = new HashMap<>();
		weathers.put("晴","0");weathers.put("晴","1");weathers.put("少云","2");
		weathers.put("多云","3");weathers.put("阴天","4");weathers.put("小雨","5");
		weathers.put("中雨","6");weathers.put("大雨", "7");weathers.put("暴雨", "8");
		weathers.put("大暴雨","9");weathers.put("特大暴雨","10");weathers.put("雷阵雨","11");
		weathers.put("浮尘","12");weathers.put("扬沙","13");weathers.put("小雪","14");
		weathers.put("小雪","15");weathers.put("中雪","16");weathers.put("大雪","17");
		weathers.put( "暴雪","18"); weathers.put( "冻雨","19"); weathers.put( "沙尘暴","20");
		weathers.put("雷阵雨伴有冰雹","21"); weathers.put( "强沙尘暴","22"); weathers.put( "雾","23");
		weathers.put( "阵雪","24"); weathers.put("阵雨","25"); weathers.put( "小雨到中雨","26");
		weathers.put( "中雨到大雨","27"); weathers.put( "大雨到暴雨","28"); weathers.put( "暴雨到大暴雨","29");
		weathers.put( "大暴雨到特大暴雨","30"); weathers.put( "小雪到中雪","31"); weathers.put( "中雪到大雪","32");
		weathers.put("大雪到暴雪","33"); weathers.put( "霾","53"); weathers.put("", "-999");
		
		return weathers.get(condition);
	}
	
	public static String windDirect(double windDirect) {
		final String[] winds = {"北风", "东北偏北风", "东北风", "东北偏东风", "东风", 
				"东南偏东风", "东南风", "东南偏南风", "南风", "西南偏南风",
				"西南风", "西南偏西风", "西北偏西风", "西北风", "西北偏北风",
				"西北偏北风"};
		final double[] windLimit = {11.25, 33.75, 56.25, 78.75, 101.25,
				123.75, 146.25, 168.75, 191.25, 213.75,
				236.25, 258.75, 281.25, 326.25, 348.75,
				1000};
		
		int maxLevel = winds.length - 1;
		int level = 0;
		String wind = "西北偏北风";
		
		while(wind.equals(winds[winds.length - 1]) && level < maxLevel) {
			if (windDirect < windLimit[level]) {
				wind = winds[level];
			}
			level++;
		}
		
		return wind;
	}
	
	
	
	public static String windgrage(double windspeed) {
		final double[] rangeLimit = { 0.3, 1.6, 3.4, 5.5, 8.0, 
				10.8, 13.9, 17.2, 20.8, 24.5,
				28.5, 32.7, 37.0, 41.5, 46.2,
				51.0, 56.1, Double.MAX_VALUE };
		final String[] grade = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
				"16", "17" };

		int maxGradeLevel = grade.length - 1;
		int gradeLevel = 0;
		String windGrade = "17";

		while (windGrade.equals("17") && gradeLevel < maxGradeLevel) {
			if (windspeed < rangeLimit[gradeLevel]) {
				windGrade = grade[gradeLevel];
			}
			gradeLevel = gradeLevel + 1;
		}
		
		return windGrade;
	}
	
	public static String windgrageToSpeed(int grade) {
		
		final double[] rangeLimit = { 0.3, 1.6, 3.4, 5.5, 8.0, 
				10.8, 13.9, 17.2, 20.8, 24.5,
				28.5, 32.7, 37.0, 41.5, 46.2,
				51.0, 56.1};
		final int[] grades = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
				16};
		Map<Integer, Double> winds = new HashMap<>();
		for (int i = 0; i < grades.length; i++) {
			winds.put(grades[i], rangeLimit[i] + 0.01);
		}
		
		return winds.get(grade).toString();
	}

	public static void main(String[] args) {
		WindGradeUtil gradeUtil=new WindGradeUtil();
		System.out.println(gradeUtil.windgrage(3.5));
	}
}
